package com.csun.cmny.provider.device.card;

import com.csun.cmny.config.properties.CmnyProperties;
import com.csun.cmny.provider.device.relay.RelayNettyTcpChannelHandler;
import com.csun.cmny.provider.device.relay.RelayNettyTcpServer;
import com.csun.cmny.provider.netty.NettyInitRunner;
import io.netty.bootstrap.ServerBootstrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-11-28 20:42
 * @Description:
 */
@Slf4j
@Order(35)
@Configuration
@ConditionalOnProperty(prefix = "cmny.netty.card", name = "switch", havingValue = "on")
public class CardNettyConfig {

    @Resource
    private CmnyProperties cmnyProperties;
    @Resource
    private CardNettyTcpChannelHandler cardNettyTcpChannelHandler;

    @Bean
    public CardNettyTcpServer cardNettyTcpServer() {

        int port = cmnyProperties.getNetty().getCard().getPort();

        log.info("### card init runner starting...<<<<<<<<<<<<<");

        CardNettyTcpServer cardNettyTcpServer =
                new CardNettyTcpServer(port, cardNettyTcpChannelHandler, new ServerBootstrap());

        cardNettyTcpServer.init();

        NettyInitRunner.nettyServerMap.put(port, cardNettyTcpServer);

        log.info("### card init runner finish...<<<<<<<<<<<<<");

        return cardNettyTcpServer;
    }

}
